package com.glb.app.config;


import com.glb.app.store.MongodbChatMemoryStore;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

@Configuration
public class XiaoZhiAgentConfig {

    @Autowired
    private MongodbChatMemoryStore mongodbChatMemoryStore;

    @Autowired
    private EmbeddingModel embeddingModel;

    @Autowired
    private EmbeddingStore embeddingStore;

    @Bean
    public ChatMemoryProvider qwenChatMemoryProviderXiaoZhi() {
        return memoryId-> MessageWindowChatMemory.builder()
                .id(memoryId).maxMessages(20)
                .chatMemoryStore(mongodbChatMemoryStore)
                .build();
    }

//    @Bean
//    ContentRetriever contentRetrieverXiaoZhi(){
//        Document document1 = FileSystemDocumentLoader.loadDocument("D:\\javaspace\\java-ai-langchain4j\\src\\main\\resources\\knowledge\\医院信息.md");
//        Document document2 = FileSystemDocumentLoader.loadDocument("D:\\javaspace\\java-ai-langchain4j\\src\\main\\resources\\knowledge\\神经内科.md");
//        Document document3 = FileSystemDocumentLoader.loadDocument("D:\\javaspace\\java-ai-langchain4j\\src\\main\\resources\\knowledge\\科室信息.md");
//        Document document4 = FileSystemDocumentLoader.loadDocument("D:\\javaspace\\java-ai-langchain4j\\src\\main\\resources\\knowledge\\口腔科.md");
//
//        List<Document> documents = Arrays.asList(document1, document2, document3,document4);
//        InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
//        EmbeddingStoreIngestor.ingest(documents, embeddingStore);
//        return EmbeddingStoreContentRetriever.from(embeddingStore);
//    }

    @Bean
    ContentRetriever contentRetrieverXiaoZhiPincone(){

        return EmbeddingStoreContentRetriever.builder()
                .embeddingModel(embeddingModel)
                .embeddingStore(embeddingStore)
                .maxResults(1)
                .minScore(0.8)
                .build();
    }



}
